package com.osmino.contacts.groups.edit;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.osmino.contacts.entity.Group;
import com.osmino.core.GroupEditItemController;

public class GroupEditAdapter extends BaseAdapter {
	
	protected final GroupEditItemController mController;
	protected final Context mContext;
	protected List<Group> mList;
	
	public GroupEditAdapter(Context context, GroupEditItemController controller){
		mContext = context;
		mController = controller;
		mList = new ArrayList<Group>();
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		if (convertView == null) {
			item = newView(position);
		} else {
			item = convertView;
		}
		bindView(position, item);
		return item;
	}
	
	protected View newView(int position) {
		GroupEditItem item = new GroupEditItem(mContext, mController);
		return item;
	}
	
	protected void bindView(int position, View view) {
		GroupEditItem groupItem = (GroupEditItem) view;
		Group group = (Group) mList.get(position);
//		groupItem.setInfo(new GroupEditInfo(group.id, group.getIconResource(), group.title, group.accountName));
		groupItem.setGroup(mList.get(position));
	}
	
	public void refresh(List<Group> groups){
		mList.clear();
		mList.addAll(groups);
		notifyDataSetChanged();
	}
}
